// pages/zhaon_xuanzhe/zhaon_xuanzhe.js
import {
  Request
} from "../../utils/request.js";
const app = getApp();

var hclist = new Array;
var listtype = 1;//当前列表类型(我的/想要)
var key = "show_list";
var key2 = "chose_list";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    buxian: false
  },

  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function (options) {
    console.log(options);
    var columid = options.columid;//列表id
    var listtype = options.listtype;//列表类型，1为单选2为多选
    var opt_type = options.opttype;//基本信息还是高级信息
    var opttype = options.cl;//已选
    if(opt_type==1){
      var cname=options.cname;
    }else{
      var cname="";
    }

    this.setData({
      listtype: listtype,
      opt_type: opt_type,
      columid: columid,
      cname: cname
    })

    new Request('Zycolumnlist/getuseropt').request(
      () => {
        return {
          userId: app.globalData.userid,
          columid: columid,
          opt_type:opt_type,
          listtype: listtype
        }
      },
      (res) => {
        console.log("请求了新数据");
        console.log(res);
        this.setData({
          optlist: res.data
        })
      });
  },
  /**
   * 多选
   */
  select_btn:function(e){
    console.log("点击按钮");
    var num = e.currentTarget.dataset.num;//获得按钮下标
    var buxian=this.data.buxian;
    //判断不限是否开启
    if(buxian==false){
      var lslist = new Array;
      lslist=this.data.optlist;
      console.log(lslist);
      var lsstate = lslist[num]['states'];
      if(lsstate==0){
        lslist[num]['states']=1;
      }else{
        lslist[num]['states']=0;
      }
      this.setData({
        optlist: lslist
      })
    }
  },
  /**
   * 单选
   */
  select_btn2:function(e){
    var num = e.currentTarget.dataset.num;//获得按钮下标
    var lslist = new Array;
    lslist=this.data.optlist;
    var lslong = lslist.length;
    console.log(lslong);
    for(var i=0;i<lslong;i++){
      var lsstate2=lslist[i]['states'];
      if(i == num){
        lslist[i]['states']=1;
      }else{
        lslist[i]['states']=0;
      }
    }
    console.log(lslist);
    this.setData({
      optlist: lslist
    })
  },

  /**
   * 不限按钮显示
   */
  _buxian:function(){
    if(this.data.buxian == true){
      this.buxians = false;
    }else if(this.data.buxian == false){
      this.buxians = true;
      var lslist = new Array;
      lslist=this.data.optlist;
      var lslong = lslist.length;
      console.log(lslong);
      for(var i=0;i<lslong;i++){
        lslist[i]['states']=0;
      }
      console.log(lslist);
      this.setData({
        optlist: lslist
      })
    }
    this.setData({
      buxian: this.buxians
    });
  },
  /**
   * 确定按钮
   */
  save_btn:function(e){
    var opt_type=this.data.opt_type;
    var list=this.data.optlist;
    var listlong=list.length;
    var uplist=new Array();
    var uplist_num=0;
    var that=this;
    for(var i=0;i<listlong;i++){
      if(list[i]['states']==1){
        uplist[uplist_num]=list[i]['id'];
        uplist_num++;
      }
      
    }
    // uplist=JSON.stringify(uplist);
    
    console.log(uplist);
    new Request('Zycolumnlist/editinfo').request(
      () => {
        return {
          openid: app.globalData.openid,
          userId: app.globalData.userid,
          columid: that.data.columid,
          opt_type: that.data.opt_type,
          listtype: that.data.listtype,
          uplist: uplist,
          cname: that.data.cname
        }
      },
      (res) => {
        console.log("请求了新数据");
        console.log(res);
        wx.navigateBack({//返回
          delta: 1
        })
      });

  },
  /**
   * 跳转首页 知遇
   */
  _button:function(){
    wx.switchTab({
      url: '/pages/zhaon_home/zhao_home',
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

    console.log("萨瓦迪卡");
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})